<?php

namespace App\Http\Service;


use App\Util\Result;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

trait ServiceUpload{
    /**
     * 验证文件
     * @author gaoyoulong
     * @param $file
     * @param $allowType
     * @param $allowSize
     * @return Result
     */
    public static function valid($file,$allowType=null,$allowSize=null){
        if($file->getError() !== UPLOAD_ERR_OK){
            return Result::failure($file->getErrorMessage());
        }

        //检查文件是否有效
        if (!$file->isValid()) {
            return Result::failure('文件失效');
        }

        $extension = $file->getClientOriginalExtension();
        if(empty($allowType)){
            $allowType = ['jpg','jpeg','gif','png'];
        }

        if(!in_array($extension,$allowType)){
            return Result::failure('图片格式不对');
        }

        $size = $file->getClientSize();
        if(empty($allowSize)){
            $allowSize = 2*1024*1024;
        }
        if($size > $allowSize){
            return Result::failure('上传图片大于设定限制');
        }

        return Result::success();
    }

    /**
     * 上传文件到本地
     * @author gaoyoulong
     * @param $file
     * @param $prefix
     * @return Result
     */
    public static function toLocal($file,$prefix){
        $contents = file_get_contents($file->getPath().DIRECTORY_SEPARATOR.$file->getFilename());
        $fileName = time().'_'.substr(md5($file->getClientOriginalName()),0,10).'.'.$file->getClientOriginalExtension();
        $tempPath = '/public/'.$fileName;
        Storage::put($tempPath,$contents);
        $res = $file->move(public_path().'/uploads/pic/'.$prefix,$tempPath);
        @unlink(storage_path().'/app'.$tempPath);
        $filePath = $prefix.DIRECTORY_SEPARATOR.$fileName;
        if($res !== false){
            return Result::success('上传成功！')->setData($filePath);
        }
        else{
            return Result::failure('上传失败，请重新尝试！');
        }
    }

    /**
     * 上传文件到本地
     * @author czs
     * @param $file
     * @param $prefix
     * @return Result
     */
    public static function toLocal2($file,$prefix){
        $contents = file_get_contents($file->getPath().DIRECTORY_SEPARATOR.$file->getFilename());
        $fileName = "excel.".$file->getClientOriginalExtension();
        $filePath = $prefix.DIRECTORY_SEPARATOR.$fileName;
        $res = Storage::put($filePath,$contents);
        if($res !== false){
            return Result::success('上传成功！')->setData($fileName);
        }
        else{
            return Result::failure('上传失败，请重新尝试！');
        }
    }

    /**
     * 生成缩略图
     * @author gaoyoulong
     * @param $fileName
     * @param $originalPrefixPath
     * @param $thumbPrefixPath
     * @param $sizeArr
     */
    public static function makeThumbPic($fileName,$originalPrefixPath,$thumbPrefixPath,$sizeArr){
        $filePath = config('filesystems.disks.local.root').DIRECTORY_SEPARATOR.$originalPrefixPath.DIRECTORY_SEPARATOR.$fileName;  //原始路径
        //生成缩略图
        if(empty($sizeArr)){
            $sizeArr = config('config.smallPicSize');  //缩略图大小
        }
        $image = Image::make($filePath)->resize($sizeArr[0],null, function ($constraint) {
            $constraint->aspectRatio();//高度auto
        });

        $smallPicPath = config('filesystems.disks.local.root').DIRECTORY_SEPARATOR.$thumbPrefixPath.DIRECTORY_SEPARATOR.$fileName;  //缩略图路径

        //判断路径
        $dir = dirname($smallPicPath);
        if (!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }

        $image->save($smallPicPath);
    }
}


